package cn.wenlihelp.controller.article;


import cn.wenlihelp.bean.Article;
import cn.wenlihelp.model.Result;
import cn.wenlihelp.model.ResultPage;
import cn.wenlihelp.service.ArticleService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;
    @RequestMapping
    public ModelAndView home(){
        return new ModelAndView("/article/index");
    }

    @GetMapping(produces = "application/json")
    public ResultPage<Article> findAll(Article article,
                                   @RequestParam(defaultValue = "1") int page,
                                   @RequestParam(defaultValue = "4") int pageSize){
        PageInfo<Article> pageInfo = articleService.findAll(page, pageSize, article);
        //return Result.success(pageInfo.getTotal(),pageInfo.getList());
        return ResultPage.success(pageInfo.getTotal(),pageInfo.getList(),pageInfo.getPageNum(),pageInfo.getPageSize());
    }

    @PostMapping("/add")
    public Result add(@RequestParam("content") String content){
        System.out.println("进来了");
        if(articleService.add(new Article(17,17,content))==1){
            System.out.println("成功了");
            return Result.success((List<Object>) null).setMsg("新增成功");
        }else{
            System.out.println("失败了");
            return new Result<>().setCode(10002).setMsg("新增失败");
        }


    }





}
